//
//  YCClassStyleCell.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/29.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  班型cell

#import "YCClassStyleCell.h"


@interface YCClassStyleCell()

/** 点击详情介绍按钮要执行的代码块  */
@property (nonatomic, copy) detailButtonClickActionBlock detailClickBlock;

/** 背景视图  */
@property (nonatomic, strong) UIView *bgView;
/** 选中图片  */
@property (nonatomic, strong) UIImageView *selectedImageView;

/** 图片 */
@property (nonatomic, weak) UIImageView *iconImageView;
/** 班级名称  */
@property (nonatomic, weak) UILabel *classLabel;
/** 班级说明  */
@property (nonatomic, weak) UILabel *classDescLabel;
/** 详细介绍按钮 */
@property (nonatomic, weak) UIButton *detailButton;

@end

@implementation YCClassStyleCell

- (void)setItemModel:(YCClassStyleModel *)itemModel
{
    _itemModel = itemModel;
    if (!itemModel.desc) {
        self.bgView.backgroundColor = itemModel.isSelected ? [UIColor yc_colorWithUInt:0xd0d0d0]:[UIColor whiteColor];
        self.selectedImageView.hidden = itemModel.isSelected ? NO : YES;
        self.detailButton.hidden = itemModel.isSelected ? YES : NO;
    }
    self.classLabel.textColor = [_itemModel.title isEqualToString:@"vip班"]?[UIColor yc_colorWithUInt:0xff4055]:[UIColor blackColor];
    
    [self.iconImageView yc_setWebImageWithURLString:itemModel.img holderSize:CGSizeMake(57, 57)];
    self.classLabel.text = itemModel.title;
    self.classDescLabel.text = itemModel.des;
}

/**
 *  快速创建cell
 *
 *  @param tableView cell所在的表
 *  @param indexPath cell位置
 *  @param action    点击详细介绍按钮的操作
 *
 *  @return 返回cell对象
 */
+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath detailButtonActionBlock:(detailButtonClickActionBlock)action
{
    static NSString *identifier = @"classStyleCell";
    YCClassStyleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[YCClassStyleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
        cell.detailClickBlock = action;
    }
    return cell;
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.selectedImageView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(13);
            make.right.equalTo(self.contentView).offset(-13);
            make.top.equalTo(self.contentView).offset(6);
            make.bottom.equalTo(self.contentView).offset(-6);
        }];
        
        [self.selectedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.width.equalTo(@50);
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView).offset(-31);
        }];
    }
    return self;
}

- (UIImageView *)selectedImageView
{
    if (!_selectedImageView) {
        _selectedImageView = [[UIImageView alloc] init];
        _selectedImageView.image = [UIImage imageNamed:@"ms_select"];
        _selectedImageView.contentMode = UIViewContentModeCenter;
        _selectedImageView.hidden = YES;
    }
    return _selectedImageView;
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
        
        UIImageView *iconImageView = [[UIImageView alloc] init];
        iconImageView.contentMode = UIViewContentModeCenter;
        iconImageView.image = [UIImage imageNamed:@"特惠班"];
        self.iconImageView = iconImageView;
        
        // 班级名称
        UILabel *classLabel = [[UILabel alloc] init];
        classLabel.font = [UIFont systemFontOfSize:16];
        classLabel.textAlignment = NSTextAlignmentLeft;
        classLabel.text = @"特惠班";
        classLabel.textColor = [UIColor blackColor];
        self.classLabel = classLabel;
        
        // 班级说明
        UILabel *classDescLabel = [[UILabel alloc] init];
        classDescLabel.font = [UIFont systemFontOfSize:14];
        classDescLabel.textAlignment = NSTextAlignmentLeft;
        classDescLabel.text = @"集中式教学培训";
        classDescLabel.textColor = [UIColor yc_colorWithUInt:0x808080];
        self.classDescLabel = classDescLabel;
        
        // 详细按钮
        UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [detailButton setTitle:@"详细介绍" forState:UIControlStateNormal];
        [detailButton setTitleColor:[UIColor yc_colorWithUInt:0x999999] forState:UIControlStateNormal];
        detailButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [detailButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        detailButton.layer.borderWidth = 1;
        detailButton.layer.borderColor = [UIColor yc_colorWithUInt:0x999999].CGColor;
        detailButton.layer.cornerRadius = 3;
        detailButton.layer.masksToBounds = YES;
        [detailButton addTarget:self action:@selector(detailButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.detailButton = detailButton;
        
        [_bgView addSubview:iconImageView];
        [_bgView addSubview:classLabel];
        [_bgView addSubview:classDescLabel];
        [_bgView addSubview:detailButton];
        
        // 布局
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bgView).offset(15);
            make.centerY.equalTo(_bgView);
            make.width.height.equalTo(@57);
        }];
        
        [classLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconImageView.mas_right).offset(15);
            make.bottom.equalTo(iconImageView.mas_centerY).offset(-4);
        }];
        
        [classDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconImageView.mas_right).offset(15);
            make.top.equalTo(classLabel.mas_bottom).offset(9);
        }];
        
        [detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bgView).offset(-heightFromIphone6Height(18));
            make.centerY.equalTo(_bgView);
            make.width.equalTo(@80);
            make.height.equalTo(@22);
        }];
        
    }
    return _bgView;
}

- (void)detailButtonClick:(UIButton *)detailButton
{
    !self.detailClickBlock ? :self.detailClickBlock();
}

@end
